function [dataFileName,taskPhase,cbal, id] = gatherSubInfo(tag, id, session)
% GATHERSUBINFO requests information about subject number and ID code.
% Creates a unique filename for saving the data.  Returns some relevant
% info in dataHeader.
% Input:  TAG is the experiment name to be used in the data file.  (a text
% string)

% load the subject progress data structure
subjectProgress = load('subjectProgress.mat');

% if no id is specified, ask for a subject ID
if nargin<2|isempty(id)
    prompt=1;
    id = [];
    while isempty(id)
        id = input('Subject ID:  ','s');
    end
else
    prompt=0;
end


% try to use the subject progress structure to determine phase and cbal
phaseLabels = {'demo', 'heliVis', 'cloudy', 'main'};
taskPhase = [];
cbal = [];
if ~isfield(subjectProgress,id) % if a new subject, initialize
    subjectProgress.(id).cbal1 = [];
    subjectProgress.(id).heliVis = [false, false];
    subjectProgress.(id).cloudy = [false, false];
    subjectProgress.(id).main = [false, false,];
elseif ~isempty(subjectProgress.(id).cbal1)
    % if the subject already exists and has cbal condition assigned
    % stop at the first phase with an incomplete segment
    for i = 1:length(phaseLabels)
        lab = phaseLabels{i};
        undone = ~subjectProgress.(id).(lab);
        if any(undone)
            bkIdx = find(undone,1,'first');
            taskPhase = lab;
            if mod(bkIdx,2)==1 % odd #'d bks get cbal1
                cbal = subjectProgress.(id).cbal1;
            else % even #'d bks get the other cbal (1 or 2)
                cbal = 3 - subjectProgress.(id).cbal1;
            end
            
            if prompt==1
                % allow info to be revised manually
                ok = input(sprintf('next up:  phase=%s, bk=%d, cbal=%d \n  (ENTER to proceed, or 1 to modify):  ',...
                    taskPhase,bkIdx,cbal));
                if ~isempty(ok), taskPhase = []; cbal = []; end
                break; % stop here w/o checking any later task phases
            else
                break;
           end      
        end % if statement
    end % loop over phases
end % if statement

% ask for the task phase
while isempty(taskPhase)
    fprintf('Task phase:\n');
    for i = 1:length(phaseLabels)
        fprintf('  %d = %s\n',i,phaseLabels{i});
    end
    if prompt==1
        phaseIdx = input('Enter the phase:  ');
        if ismember(phaseIdx,1:length(phaseLabels))
            taskPhase = phaseLabels{phaseIdx};
        end
    end   
end

% ask for the counterbalance condition
%%% NOTE: may implement smart defaults
while isempty(cbal)
    cbal = input('Enter the cbal:  ');
    if ~ismember(cbal,1:2), cbal = []; end
end
if isempty(subjectProgress.(id).cbal1) % store cbal for later use
    subjectProgress.(id).cbal1 = cbal;
end

% set session number, ensuring a unique filename


if nargin<3||isempty(session)
session = 1;
end

nameVetted = false;
while ~nameVetted
    dataFileName = fullfile('data',sprintf('%s_%s_%s_%d',tag,id,taskPhase,session));
    if exist(sprintf('%s.mat',dataFileName),'file')==2
        session = session+1;
    elseif exist(sprintf('%s.txt',dataFileName),'file')==2
        session = session+1;
    else
        nameVetted = true;
    end
end

if prompt==1
input(['Data will be saved in:\n  ',dataFileName,'\n  (ENTER to continue)']);
end

% mark the current segment as complete
subjectProgress.(id).(taskPhase)(session) = true;

% save the subject progress data structure
save('subjectProgress.mat','-struct','subjectProgress');


